﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Guestbook
{
    public partial class ViewGuests : Form
    {
        public ViewGuests()
        {
            InitializeComponent();
        }

        private void ViewGuests_Load(object sender, EventArgs e)
        {
            var data = LoadGuestBook();

            if (data != null)
            {
                Populate(data.Guest.ToList());
            }
        }

        private void Populate(IEnumerable<GuestbookData.GuestRow> rows)
        {
            grid.DataSource = rows;
            foreach (DataGridViewColumn item in grid.Columns)
            {
                if (item.Name != "Name" && item.Name != "Email" && item.Name != "Comment"  && item.Name != "SignedIn")
                {
                    item.Visible = false;
                }
            }
        }

        private GuestbookData LoadGuestBook()
        {
            if (System.IO.File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "GuestBook.xml"))
            {
                var guestbookData = new GuestbookData();
                guestbookData.ReadXml(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "GuestBook.xml");
                return guestbookData;
            }

            return null;
        }

        private void btnFilter_Click(object sender, EventArgs e)
        {
            var book = LoadGuestBook();

            if (Start.Value <= End.Value && book != null)
            {
                var values = (from x in book.Guest where x.SignedIn >= Start.Value.Date && x.SignedIn <= (End.Value.Date + new TimeSpan(0, 23, 59, 59)) select x).ToList();
                Populate(values);
            }
        }

        private void guestbookDataBindingSource_CurrentChanged(object sender, EventArgs e)
        {

        }
    }
}
